package com.ruoyi.web.controller.shop.trade;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.delivery.service.IShopExpressService;
import com.ruoyi.shop.trade.domain.ShopOrder;
import com.ruoyi.shop.trade.domain.ShopOrderDelivery;
import com.ruoyi.shop.trade.domain.ShopOrderHistory;
import com.ruoyi.shop.trade.mapper.ShopOrderDeliveryMapper;
import com.ruoyi.shop.trade.service.IShopOrderHistoryService;
import com.ruoyi.shop.trade.service.IShopOrderItemService;
import com.ruoyi.shop.trade.service.IShopOrderService;
import com.ruoyi.shop.trade.vo.ShopOrderItemVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商城订单Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/trade/order")
public class ShopOrderController extends BaseController {
	private String prefix = "shop/trade/order";

	@Autowired
	private IShopOrderService shopOrderService;
	@Autowired
	private IShopOrderItemService shopOrderItemService;
	@Autowired
	private IShopOrderHistoryService shopOrderHistoryService;
	
	@Autowired
	private IShopExpressService shopExpressService;
	@Autowired
	private ShopOrderDeliveryMapper shopOrderDeliveryMapper;

	@RequiresPermissions("shop:order:view")
	@GetMapping()
	public String order() {
		return prefix + "/order";
	}

	/**
	 * 查询商城订单列表
	 */
	@RequiresPermissions("shop:order:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ShopOrder shopOrder) {
		startPage();
		List<ShopOrder> list = shopOrderService.selectOrderList(shopOrder);
		return getDataTable(list);
	}

	/**
	 * 导出商城订单列表
	 */
	@RequiresPermissions("shop:order:export")
	@Log(title = "商城订单", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ShopOrder shopOrder) {
		List<ShopOrder> list = shopOrderService.selectOrderList(shopOrder);
		ExcelUtil<ShopOrder> util = new ExcelUtil<ShopOrder>(ShopOrder.class);
		return util.exportExcel(list, "商城订单数据");
	}

	/**
	 * 修改商城订单
	 */
	@RequiresPermissions("shop:order:edit")
	@GetMapping("/edit/{orderId}")
	public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap) {
		ShopOrder shopOrder = shopOrderService.selectOrderById(orderId);
		mmap.put("shopOrder", shopOrder);
		return prefix + "/edit";
	}

	/**
	 * 修改保存商城订单
	 */
	@RequiresPermissions("shop:order:edit")
	@Log(title = "商城订单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopOrder shopOrder) {
		return toAjax(shopOrderService.updateOrder(shopOrder));
	}

	/**
	 * 删除商城订单
	 */
	@RequiresPermissions("shop:order:remove")
	@Log(title = "商城订单", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(shopOrderService.deleteOrderByIds(ids));
	}

	/**
	 * 订单详情页
	 * 
	 * @param orderId
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("shop:order:detail")
	@GetMapping("/detail/{orderId}")
	public String detail(@PathVariable("orderId") Long orderId, ModelMap mmap) {
		ShopOrder shopOrder = shopOrderService.selectOrderById(orderId);
		mmap.put("shopOrder", shopOrder);
		List<ShopOrderItemVo> orderItems = shopOrderItemService.selectItemByOrderId(orderId);
		mmap.put("orderItems", orderItems);
		List<ShopOrderHistory> orderHistorys = shopOrderHistoryService.selectHistoryByOrderId(orderId);
		mmap.put("orderHistorys", orderHistorys);
		ShopOrderDelivery delivery = shopOrderDeliveryMapper.selectDeliveryById(shopOrder.getDeliveryId());
		if (StringUtils.isNull(delivery)) {
			delivery = new ShopOrderDelivery();
		}
		mmap.put("delivery", delivery);
		return prefix + "/detail";
	}

	/**
	 * 线下支付订单确认
	 * 
	 * @param orderId
	 * @return
	 */
	@Log(title = "支付订单", businessType = BusinessType.UPDATE)
	@PostMapping("/pay/{orderId}")
	@ResponseBody
	public AjaxResult orderPay(@PathVariable("orderId") String orderId) {
		return success(shopOrderService.payOrder(Long.valueOf(orderId)));
	}

	/**
	 * 订单配送页
	 * 
	 * @param orderId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/delivery/{orderId}")
	public String delivery(@PathVariable("orderId") Long orderId, ModelMap mmap) {
		mmap.put("order", shopOrderService.selectOrderById(orderId));
		mmap.put("expresses", shopExpressService.queryExpresses());
		return prefix + "/delivery";
	}

	/**
	 * 配送订单
	 * 
	 * @param orderId
	 * @return
	 */
	@Log(title = "配送订单", businessType = BusinessType.UPDATE)
	@PostMapping("/delivery")
	@ResponseBody
	public AjaxResult deliveryOrder(ShopOrderDelivery request) {
		return success(shopOrderService.deliveryOrder(request));
	}

	/**
	 * 订单状态数量统计
	 * 
	 * @return
	 */
	@GetMapping("/statusCount")
	@ResponseBody
	public AjaxResult orderStatusCount() {
		return success(shopOrderService.statusCount());
	}
	
	/**
	 * 备注订单
	 * 
	 * @param request
	 * @return
	 */
	@PostMapping("/remark")
	@ResponseBody
	public AjaxResult remarkOrder(ShopOrder request) {
		return success(shopOrderService.remarkOrder(request));
	}

}
